package com.goldsprite.infinityworld.entities.randommap;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.goldsprite.utils.math.Vector2Int;

import java.util.ArrayList;
import java.util.List;

public class CellAssets {
	public Texture tex;
	public int split;
	public Vector2Int gridCount = new Vector2Int();
	public List<TextureRegion> regs = new ArrayList<>();


	public CellAssets(Texture tex, int split){
		this.tex = tex;
		this.split = split;
		gridCount.x = tex.getWidth()/split;
		gridCount.y = tex.getHeight()/split;
		for(int y=0;y<gridCount.y;y++){
			for(int x=0;x<gridCount.x;x++){
				TextureRegion reg = new TextureRegion(tex);
				reg.setRegion(x*split, y*split, split, split);
				regs.add(reg);
			}
		}
	}

	public void displayAll(Batch batch) {
		float drawSize = split*0.7f;
		int margin = 2;
		float startX = 0, startY = (gridCount.y-1)*(drawSize+margin);
		for(int y=0;y<gridCount.y;y++){
			for(int x=0;x<gridCount.x;x++){
				batch.draw(getCell(x, y), startX+x*(drawSize+margin), startY-y*(drawSize+margin), drawSize, drawSize);
			}
		}
	}

	public TextureRegion getCell(String name){
		BlockId block = BlockId.valueOf(name);
		return getCell(block.x, block.y);
	}
	public TextureRegion getCell(int x, int y){
		int index = x+gridCount.x*y;
		return getCell(index);
	}
	public TextureRegion getCell(int index){
		if(index<0||index>regs.size()-1) return null;
		return regs.get(index);
	}
}
